#include <sys/ioctl.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <iostream>

const char *host="192.168.1.14";
unsigned short port = 1080;

int connect_nonb(int sockfd,
                 const struct sockaddr *saptr,
                 socklen_t salen,
                 int sec)
{
    int             flags, n, error;
    socklen_t       len;
    fd_set          rset, wset;
    struct timeval  tval;

    flags = fcntl(sockfd, F_GETFL, 0);
    fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);

    printf("connect_nonb, line, %d\n", __LINE__);
    error = 0;
    if ( (n = connect(sockfd, saptr, salen)) < 0)
        if (errno != EINPROGRESS)
            return(-1);
    printf("connect_nonb, line, %d\n", __LINE__);

    /* Do whatever we want while the connect is taking place. */

    if (n == 0)
        goto done;	/* connect completed immediately */
    printf("connect_nonb, line, %d\n", __LINE__);

    FD_ZERO(&rset);
    FD_SET(sockfd, &rset);
    wset = rset;
    tval.tv_sec = sec;
    tval.tv_usec = 0;
    printf("connect_nonb, line, %d\n", __LINE__);

    if ( (n = select(sockfd+1, &rset, &wset, NULL, sec ? &tval : NULL)) == 0) {
        errno = ETIMEDOUT;
        return(-1);
    }
    printf("connect_nonb, line, %d\n", __LINE__);

    if (FD_ISSET(sockfd, &rset) || FD_ISSET(sockfd, &wset)) {
        len = sizeof(error);
        if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
            return(-1);         /* Solaris pending error */
    }
    printf("connect_nonb, line, %d\n", __LINE__);

done:
    fcntl(sockfd, F_SETFL, flags);  /* restore file status flags */
    printf("connect_nonb, line, %d\n", __LINE__);

    if (error) {
        errno = error;
        return(-1);
    }
    printf("connect_nonb, line, %d\n", __LINE__);
    return(0);
}

void tcp_client()
{
    struct sockaddr_in servaddr;
    char tmpIP[8];
    char tmpPort[8];

    int sfd = socket(AF_INET, SOCK_STREAM, 0);
    memset(&servaddr, 0, sizeof(servaddr));

    const char * ip = host;
    int connectTimeout = 5;

    if (inet_pton(AF_INET,
                    ip,
                    &servaddr.sin_addr) == 1)
    {
        servaddr.sin_family = AF_INET;
        servaddr.sin_port   = htons(port);
    }
    else
    {
        printf("TCPClient, inet_pton error. unknow IP: %s\n", ip);
    }

    printf("TCPClient, connecting to server(%s:%d), connectTimeout: %d\n", ip, port, connectTimeout);
    if (connect_nonb(sfd,
                        (struct sockaddr*)&servaddr,
                        sizeof(servaddr),
                        connectTimeout) == 0)
    {
        printf("TCPClient, connect to server(%s:%d) OK\n", ip, port);
        sleep(60);
    }
    else
    {
        printf("TCPClient, connect to server(%s:%d) error: %s\n", ip, port, strerror(errno));
    }
}




int loop(int a)
{
    while(1)
    {




        
    }

}













int main(int argc, char *argv[])
{
    opterr=0;
    int ch;
    while ((ch=getopt(argc,argv,"p:h")) != -1)
    {
        switch (ch)
        {
        case 'p':
            port = atoi(optarg);
            std::cout << "port " << port << std::endl;
            break;
        case 'o':
            host = optarg;
            std::cout << "host " << host << std::endl;
            break;
        }
    }

    tcp_client();

    return 0;
}

